﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using EA;

namespace HSDc.EA.WorkflowDefinitionsTranformPlugIn.Resources
{
    public class EAResource
    {
        private static Hashtable RepoHt = new Hashtable();
        private static Hashtable DiagIdHt = new Hashtable();
        private static Hashtable ObjIdHt = new Hashtable();
        private static Hashtable ClassNameHt = new Hashtable();
        private static Hashtable MethodNameHt = new Hashtable();
        private static Hashtable FragmentUsedHt = new Hashtable();
        //public static Repository CurrentRepository;
        //public static int CurrentDiagramId;
        //public static int CurrentObjectId;
        //public static string ClassName;
        //public static string MethodName;

        public static Repository CurrentRepository(string id)
        {
            object value = FindOutValue(ref RepoHt, id);
            if (value == null)
                return null;
            return (Repository) value;
        }

        public static void CurrentRepository(string id, Repository repo)
        {
            AddValue(ref RepoHt, id, repo);
        }

        public static int CurrentDiagramId(string id)
        {
            object value = FindOutValue(ref DiagIdHt, id);
            if (value == null)
                return 0;
            return Convert.ToInt32(value);
        }

        public static void CurrentDiagramId(string id, int DiagramId)
        {
            AddValue(ref DiagIdHt, id, DiagramId);
        }

        public static int CurrentObjectId(string id)
        {
            object value = FindOutValue(ref ObjIdHt, id);
            if (value == null)
                return 0;
            return Convert.ToInt32(value);
        }

        public static void CurrentObjectId(string id, int ObjectId)
        {
            AddValue(ref ObjIdHt, id, ObjectId);
        }

        public static string ClassName(string id)
        {
            object value = FindOutValue(ref ClassNameHt, id);
            if (value == null)
                return null;
            return value.ToString();
        }

        public static void ClassName(string id, string ClassName)
        {
            AddValue(ref ClassNameHt, id, ClassName);
        }

        public static string MethodName(string id)
        {
            object value = FindOutValue(ref MethodNameHt, id);
            if (value == null)
                return null;
            return value.ToString();
        }

        public static void MethodName(string id, string MethodName)
        {
            AddValue(ref MethodNameHt, id, MethodName);
        }

        public static bool IsFragmentUsed(string id)
        {
            object value = FindOutValue(ref FragmentUsedHt, id);
            if (value == null)
                return false;
            return Convert.ToBoolean(value);
        }

        public static void IsFragmentUsed(string id, bool FragmentUsed)
        {
            AddValue(ref FragmentUsedHt, id, FragmentUsed);
        }
        
        public static void RemoveResource(string id)
        {
            RepoHt.Remove(id);
            DiagIdHt.Remove(id);
            ObjIdHt.Remove(id);
            ClassNameHt.Remove(id);
            MethodNameHt.Remove(id);
            FragmentUsedHt.Remove(id);
        }

        private static object FindOutValue(ref Hashtable ht, string id)
        {
            IDictionaryEnumerator de = ht.GetEnumerator();
            while (de.MoveNext())
                if (de.Key.Equals(id))
                    return de.Value;
            return null;
        }

        private static void AddValue(ref Hashtable ht, string id, object value)
        {
            if (ht.Contains(id))
                ht.Remove(id);
            ht.Add(id, value);
        }
    }
}
